package com.yusvn.scm.goods.shoudongfenhuo.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.BaseController;

import com.yusvn.scm.common.kit.MyKit;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.goods.shoudongfenhuo.service.ScmShouDongFenHuoService;



/**
 * 手动分货
 */
public class ScmShouDongFenHuoController extends BaseController {

    @Inject
    ScmShouDongFenHuoService srv;

    public void index() {
        renderJson();
    }

    /**
     * 查询需要手动分货的商品
     */
    public void getPageSdfhsp(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        Page<Record> list = srv.getPageSdfhsp(kv);
        renderJson(RetKit.ok().setCount(list.getTotalRow()).setData( JSONObject.toJSON(list.getList()) ) );
    }

    /**
     * 获取商品动态属性对应表
     */
    public void selectAllAttrib() {
        renderJson(srv.selectAllAttrib());
    }

    /**
     * 获取某个商品属性
     */
    public void selctAttribById() {
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.selctAttribById(kv));
    }

    /**
     * 根据spid, ysid 查询库存
     */
    public void selectQtycan(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.selectQtycan(kv));
    }

    /**
     * 手动分货表三查询
     */
    public void selectSdfhCz(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.selectSdfhCz(kv));
    }

    /**
     * 查询本次分货明细
     */
    public void selectBcsdfhmx(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.selectBcsdfhmx(kv));
    }

    /**
     * 新增或修改分货数量
     */
    public void insertScmSdfhjg(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.insertScmSdfhjg(kv));
    }

    /**
     * 根据门店查询分货明细
     */
    public void sdfhBymdid(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.sdfhBymdid(kv));
    }

    /**
     * 查询店仓级别
     */
    public void selectDefstoreType(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.selectDefstoreType(kv));
    }

    /**
     * 查询店仓性质
     */
    public void selectDefstoreFlag(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.selectDefstoreFlag(kv));
    }

    /**
     * 根据条件查询门店
     */
    public void selectMdList(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.selectMdList(kv));
    }

    /**
     * 整手新增或者修改分货数量
     */
    public void zsUpdateOrinsertSdfh(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.zsUpdateOrinsertSdfh(kv));
    }


    /**
     * 手动分货生成单据
     */
    public void sdfhScdj(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        // 获取当前登录用户
        kv.set("userid", getLoginUserId());
        renderJson(srv.sdfhScdj(kv));
    }

    /**
     *查询总仓存在的商品
     */
    public void getZcSp(){
        Kv kv = MyKit.jsonStrToKv(getRawData(), true);
        if (kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.getZcSp(kv));
    }

    /**
     * 获取商品图片和供应商
     */
    public void getPstpGys(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        renderJson(srv.getPstpGys(kv));
    }

    /**
     * 删除分货结果
     */
    public void deleteTableClick(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.deleteTableClick(kv));
    }

    /**
     *查询已选择的门店名称
     */
    public void selectYxzmd(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.selectYxzmd(kv));
    }

    /**
     * 新增已选择的门店
     */
    public void insertYxzmd(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.insertYxzmd(kv));
    }

    /**
     * 删除已选择门店
     */
    public void deleteYxzmd(){

        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.deleteYxzmd(kv));
    }

    /**
     * 批量修改或新增分货
     */
    public void plInsertOrUpdate(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.plInsertOrUpdate(kv));
    }

    /**
     * 根据销售数量分货
     */
    public void xsfhInsert(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.xsfhInsert(kv));
    }

    /**
     * 多类汇总方法
     */
    public void dlhzButton(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.dlhzButton(kv));
    }

    /**
     *  批量添加门店
     */
    public void plinsertMd(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.plinsertMd(kv));
    }

    /**
     * 批量删除已选择门店
     */
    public void pldeleteMd(){
        Kv kv = jsonStrToKv(getRawData()); //优先Request Payload的传值
        if(kv.isEmpty()) {
            kv = getKv();
        }
        kv.set("userid", getLoginUserId());
        renderJson(srv.pldeleteMd(kv));
    }


}
